Create Your First Functional Component in React JS – Beginner's Guide
Introduction
Variables are one of the most fundamental concepts in Python
programming. A variable stores data that can be used and modified throughout
your program. Python supports multiple built-in data types, making it easy to
work with numbers, text, collections, and boolean values.
In this tutorial, you'll learn what variables are, how to
declare them, different Python data types, naming rules, type conversion, and
practical examples suitable for beginners.
A variable is a container used to store data values.
Unlike many programming languages, Python does not require
declaring the variable type.
Example
name = "John"
age = 25
salary = 35000.50
Python automatically determines the data type.
Variables should follow these rules:
✔ Start with a letter or
underscore
name = "John"
_age = 22
✔ Cannot start with a number
❌ Wrong
1name = "John"
✔ Only letters, numbers, and
underscore are allowed.
student_name
student1
✔ Variable names are
case-sensitive.
Name = "John"
name = "David"
Both are different variables.
Example
city = "Mumbai"
marks = 95
height = 5.9
print(city)
print(marks)
print(height)
Output
Mumbai
95
5.9
Assign multiple variables at once.
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Output
10
20
30
a = b = c = 100
print(a)
print(b)
print(c)
Output
100
100
100
Python provides several built-in data types.
|
Data Type |
Description |
Example |
|
int |
Integer |
10 |
|
float |
Decimal Number |
10.5 |
|
complex |
Complex Number |
3+4j |
|
str |
Text/String |
"Hello" |
|
bool |
True/False |
True |
|
list |
Ordered Collection |
[1,2,3] |
|
tuple |
Immutable Collection |
(1,2,3) |
|
set |
Unique Values |
{1,2,3} |
|
dict |
Key-Value Pair |
{"name":"John"} |
Integer (int)
age = 20
print(age)
Output
20
price = 199.99
print(price)
Output
199.99
number = 5 + 7j
print(number)
Output
(5+7j)
Strings store text.
name = "Python"
print(name)
Output
Python
Boolean stores only two values.
isPythonEasy = True
print(isPythonEasy)
Output
True
Lists are ordered and mutable.
fruits = ["Apple", "Mango",
"Banana"]
print(fruits)
Output
['Apple', 'Mango', 'Banana']
Tuple is ordered but immutable.
colors = ("Red", "Blue",
"Green")
print(colors)
Output
('Red', 'Blue', 'Green')
Stores data as key-value pairs.
student = {
"name":
"John",
"age":
20,
"course": "Python"
}
print(student)
Output
{'name': 'John', 'age': 20, 'course': 'Python'}
Stores unique values.
numbers = {1,2,3,4,5}
print(numbers)
Output
{1,2,3,4,5}
x = 100
y = 15.5
z = "Python"
print(type(x))
print(type(y))
print(type(z))
<class 'int'>
<class 'float'>
<class 'str'>
Convert one data type into another.
age = "25"
print(int(age))
Output
25
Convert integer to string
number = 100
print(str(number))
Output
100
name = "Amit"
age = 22
height = 5.8
isStudent = True
print("Name :", name)
print("Age :", age)
print("Height :", height)
print("Student :", isStudent)
print(type(name))
print(type(age))
print(type(height))
print(type(isStudent))
Output
Name : Amit
Age : 22
Height : 5.8
Student : True
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
1. What is a variable in Python?
A variable stores data values in memory.
2. Does Python require variable declaration?
No.
3. What does type() do?
It returns the data type of a variable.
4. Difference between List and Tuple?
List is mutable, Tuple is immutable.
5. Difference between int and float?
6. What is Boolean?
A data type containing True or False.
7. Which data type stores key-value pairs?
Dictionary.
8. Which data type stores unique values?
Set.
9. Can Python variables change data type?
Yes.
Example:
x = 10
x = "Hello"
10. What is dynamic typing?
Python automatically detects the variable's data type during
runtime.
1. What is a variable in Python?
A variable is a name that stores a value in memory.
2. Which symbol is used to assign a value?
The assignment operator (=).
3. Is Python case-sensitive?
Yes, name and Name are different.
4. Can a variable store different data types?
Yes. Python is dynamically typed.
5. What are Python's main data types?
int, float, str, bool, list, tuple, set, dict, and complex.
6. What is the difference between list and set?
Lists preserve order and allow duplicates; sets store only
unique values and do not guarantee order.
7. What is a dictionary?
A collection of key-value pairs.
8. How do I check a variable's type?
Use the type() function.
9. What is type conversion?
Changing one data type into another using functions like
int(), float(), or str().
10. Why are variables important?
They allow programs to store, retrieve, and manipulate data
efficiently.
Python variables and data types are essential building blocks for every Python programmer. Understanding how to create variables, use different data types, and perform type conversion will help you write efficient and readable code. Mastering these concepts prepares you for more advanced topics like operators, conditional statements, loops, functions, and object-oriented programming.
Comments
Post a Comment